package com.marvels.financial.loan.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.ExtensionFunctionConditionDTO;
import com.marvels.financial.loan.dto.McfePaymentWithholdDetailDTO;
import com.marvels.financial.loan.dto.PaymentResponse;
import com.marvels.financial.loan.service.ExtensionFunctionApi;

/**
 * 展期功能controller
 * @author 唐佩文
 *
 */
@RestController
@RequestMapping("financial/loan/extensionFunction/service")
public class ExtensionFunctionController {

	@Autowired
	private ExtensionFunctionApi extensionFunctionApi;
	
	/**
	 * 展期功能代扣
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("extensionWithhold")
	@ResponseBody
	public MarvelsResultDTO extensionWithhold(@RequestBody ExtensionFunctionConditionDTO conditionDTO){
		try {
			PaymentResponse paymentResponse = extensionFunctionApi.extensionWithhold(conditionDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					paymentResponse);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>展期功能代扣FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>展期功能代扣异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	
	/**
	 * 展期功能代扣结果查询
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("extensionWithholdQuery")
	@ResponseBody
	public MarvelsResultDTO extensionWithholdQuery(@RequestBody ExtensionFunctionConditionDTO conditionDTO){
		try {
			PaymentResponse paymentResponse = extensionFunctionApi.extensionWithholdQuery(conditionDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					paymentResponse);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>展期功能代扣结果查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>展期功能代扣结果查询异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	
	/**
	 * 展期费记录查询
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("extensionRecordsQuery")
	@ResponseBody
	public MarvelsResultDTO extensionRecordsQuery(@RequestBody ExtensionFunctionConditionDTO conditionDTO){
		try {
			List<McfePaymentWithholdDetailDTO> detailDTOs= extensionFunctionApi.extensionRecordsQuery(conditionDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					detailDTOs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>展期功能代扣结果查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>展期功能代扣结果查询异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	
	
	
	/**
	 * 查询展期信息
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("selectExtensionInfo")
	@ResponseBody
	public MarvelsResultDTO selectExtensionInfo(@RequestBody ExtensionFunctionConditionDTO conditionDTO){
		try {
			ExtensionFunctionConditionDTO detail= extensionFunctionApi.selectExtensionInfo(conditionDTO);
			if(conditionDTO != null){
				detail.setIndex(conditionDTO.getIndex());
			}
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					detail);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>查询展期信息FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>查询展期信息异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
}
